package com.hfb.mashibing.alip8.designpattern._09observer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 题目：模拟 宝宝在睡觉，醒来后哭了，之后要喂奶
 * 面向对象的傻等，直到醒来
 */

public class V6 {

    public static void main(String[] args) throws InterruptedException {
        ChildV6 baby = new ChildV6();
        TimeUnit.SECONDS.sleep(3);
        // do something
        baby.wakeUp();

    }
}

/**
 * 加入多个观察者
 */
class ChildV6 {
    private boolean cry = false;
    /*观察者与被观察者分离*/
    private List<ObserverV6> observers = new ArrayList<ObserverV6>(){{
        add(new MamV6());
        add(new DadV6());
        add(new NaiNaiV6());
    }};
    public ChildV6() {
        System.out.println("baby is doing sleepping !");
    }

    public boolean isCry() { return cry;}

    public void wakeUp(){
        cry = true;
        System.out.println("waked up! Crying! 555555555555555555");
        WakeUpEvent event = new WakeUpEvent(System.currentTimeMillis(), "bad");
        observers.forEach(
            o -> o.actionOnWakeUp(event)
        );
    }
}

/**
 * 事件类
 */
class WakeUpEvent{
    long timstamp;
    String loc;

    public WakeUpEvent(long timstamp, String location) {
        this.timstamp = timstamp;
        this.loc = loc;
    }
}
/**
 * 加入观察者
 */
interface ObserverV6 {
    void actionOnWakeUp(WakeUpEvent t);
}

class DadV6 implements ObserverV6 {
    @Override
    public void actionOnWakeUp(WakeUpEvent t) {
        System.out.println("he is Crying! dad is go to say !");
    }
}

class MamV6 implements ObserverV6 {
    @Override
    public void actionOnWakeUp(WakeUpEvent t) {
        System.out.println("he is Crying! MamV6 is go to hug !");
    }
}

class NaiNaiV6 implements ObserverV6 {
    @Override
    public void actionOnWakeUp(WakeUpEvent t) {
        System.out.println("he is Crying! NaiNaiV6 is go to feed !");
    }
}
